home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_2 / interfaces / willy-ttx / spellfix.ttx < prev   
Text File  |  1992-11-24  |  11KB  |  419 lines

  1. /** SpellFix.ttx
  2. *
  3. *   Interface to ISpell to spellfix an entire file. To be called from
  4. *   inside Turbotext. If an argument "all" is given, then "closest root"
  5. *   errors will also be flagged.
  6. *
  7. *   W.G.J. Langeveld, April 1992.
  8. *
  9. *   Hacked by Loren to try to adhere to the new extendedfilecheck
  10. *   method instead of the custom ISpell method that Willy used.
  11. *
  12. *   Loren J. Rittle, Fri Nov 20 01:51:22 1992
  13. *
  14. *   Fixed by Willy to actually work again... 8^)
  15. *
  16. *   W.G.J. Langeveld, Nov 25 1992.
  17. *
  18. **/
  19. parse arg allfix
  20.      
  21. options results
  22. options failat 100
  23. /*
  24. *   Allow no interruptions so we can cleanup
  25. */
  26. SIGNAL ON BREAK_C
  27. SIGNAL ON BREAK_D
  28. SIGNAL ON BREAK_E
  29. SIGNAL ON BREAK_F
  30. SIGNAL ON FAILURE
  31. SIGNAL ON HALT
  32. SIGNAL ON SYNTAX
  33. /*
  34. *   Get the screen size and name according to TTX. Set the TTX window
  35. *   size such that there is room for the requester.
  36. */
  37. "getscreeninfo"
  38. info = result
  39. parse var info x x x x x cols rows myspellscreen
  40. /*
  41. *   Check if file has been saved. If not, save it.
  42. */
  43. "getfileinfo"
  44. parse var result x changes x .
  45. if changes = "YES" then "savefile"
  46. /*
  47. *   Change TTX's window size and put up the spellfix requester
  48. */
  49. call SpellRequest(cols, rows, myspellscreen)
  50. /*
  51. *   Start ISpell if it isn't running yet.
  52. */
  53. if ~showlist("p", "IRexxSpell") then do
  54.    call SpellMsg("ISpell not running, starting ISpell")
  55.    address COMMAND "run >nil: <nil: ISpell -r"
  56.    do i = 1 to 50
  57.       call delay(50)
  58.       if showlist("p", "IRexxSpell") then leave i
  59.    end
  60.    if ~showlist("p", "IRexxSpell") then signal ERROR:
  61. end
  62. /*
  63. *   Get the file name, spellcheck the file quickly.
  64. */
  65. "GetFilePath"
  66. name = result
  67. address "IRexxSpell" extendedfilecheck name
  68.      
  69. "SetDisplayLock ON"
  70. spellword. = ""
  71. do i = 1 to ispellresult.count
  72. /*
  73. *   Get location of spell error
  74. */
  75.    spellword.i = ispellresult.i.word
  76.    y = ispellresult.i.line
  77.    x = ispellresult.i.column
  78. /*
  79. *   Get the line it is in
  80. */
  81.    "Move Folds" y 1
  82.    "GetLine"
  83.    fileline = result
  84. /*
  85. *   If the spell error is somewhere in the line, untab the part
  86. *   before it to find the real column number.
  87. */
  88.    if x ~= 1 then do
  89.       firstpart = substr(fileline, 1, x - 1)
  90.       firstpart = untab(firstpart)
  91.       x = length(firstpart) + 1
  92.    end
  93. /*
  94. *   Now set a bookmark there.
  95. */
  96.    "Move Folds" y x
  97.    "SetBookmark" 1000 + i
  98. end
  99.      
  100. /*
  101. *   Get number of lines, etc. and move to the start of the file.
  102. */
  103. "GetFileInfo"
  104. parse var result nlines mods name
  105. "MoveSOF"
  106. test = 1
  107.      
  108. /*
  109. *   Loop over the errors (bookmarks).
  110. */
  111. do i = 1 to ispellresult.count
  112.    "MoveBookmark" 1000 + i
  113.      
  114.    item = spellword.i
  115. /*
  116. *   See if the word under the cursor is indeed what we think it is.
  117. *   If not, see if this line *has* a word like that.
  118. */
  119.    "GetWord"
  120.    if result ~= item then do
  121.       "GetLine"
  122.       fileline = result
  123.       fileline = untab(fileline)
  124.       fileline = translate(fileline,, "[]{}(),.;:/<>:?`~!@#$%^&*_+|-=\|"||'"'||'0a'x||'09'x)
  125. /*
  126. *   Loop over the words in the line
  127. */
  128.       place = 0
  129.       n = words(fileline)
  130.       do j = 1 to n
  131.          if word(fileline, j) = item then do
  132.             place = wordindex(fileline, j)
  133.             leave j
  134.          end
  135.       end
  136. /*
  137. *   If the line no longer has the word, we assume it was fixed.
  138. */
  139.       if place = 0 then iterate i
  140. /*
  141. *   Otherwise, we'll position ourselves at that word.
  142. */
  143.       "GetCursorPos"
  144.       parse var result y x .
  145.       "Move folds" y place
  146.    end
  147.      
  148.    address "IRexxSpell" check item
  149. /*
  150. *   Find out what ISpell thought about this one
  151. */
  152.    r = result
  153.    r1 = substr(r, 1, 1)
  154.    string = ""
  155.    alternatives = ""
  156.    select
  157.       when r1 = '*' then do
  158.       end
  159.       when r1 = '&' then do
  160.          string = '"'item||'" not found. Click on Select for alternatives.'
  161.          alternatives = substr(r, 3)
  162.       end
  163.       when r1 = '#' then do
  164.          string = '"'item||'" not found.'
  165.       end
  166.       when r1 = '+' then do
  167.          if allfix ~= "" then do
  168.             string = '"'||item||'" not found. Closest root:' substr(r, 3)
  169.          end
  170.       end
  171.       otherwise string = '"'||item||'":' r
  172.    end
  173. /*
  174. *   If we have a result string, then put the cursor at the offending
  175. *   location and ask the user what to do about it.
  176. */
  177.    if string ~= "" then do
  178.       "SetDisplayLock OFF"
  179.       test = GetNextMsg(item, string, alternatives)
  180.       if test = 0 then leave i
  181.       "SetDisplayLock ON"
  182.    end
  183. end
  184.      
  185. /**
  186. *
  187. *   Clean up
  188. *
  189. **/
  190. BREAK_C:
  191. BREAK_D:
  192. BREAK_E:
  193. BREAK_F:
  194. ERROR:
  195. FAILURE:
  196. HALT:
  197. SYNTAX:
  198.      
  199.    "SetDisplayLock OFF"
  200.    if showlist('p', 'SPELLREQUEST') then do
  201. /*
  202. *   End of document. Give user chance to do more stuff.
  203. */
  204.       if test ~= 0 then do i = 1
  205.          test = GetNextMsg(" ", "All done!", "")
  206.          if test = 0 then leave i
  207.       end
  208.      
  209.       call CloseWindow(SPELLREQUEST)
  210.       "MoveWindow 0 -55"
  211.       "SizeWindow 0 55"
  212.    end
  213.    exit
  214.      
  215.      
  216. /** untab
  217. *
  218. *   Expand tabs in a line.
  219. *
  220. **/
  221. untab: Procedure
  222.    parse arg line
  223.      
  224.    "getprefs tabwidth"
  225.    tabsetting = result
  226.      
  227.    do forever
  228.       n = index(line, '09'x)
  229.       if n = 0 then return line
  230.       nspaces = tabsetting - ((n - 1) // tabsetting)
  231.       line = insert(" ", delstr(line, n, 1), n - 1, nspaces, " ")
  232.    end
  233.      
  234.      
  235. /** SpellRequest
  236. *
  237. *   This program brings up a spell requester
  238. *
  239. *   Add the libraries if they're not there yet.
  240. *
  241. **/
  242. SpellRequest: Procedure
  243. parse arg cols, rows, myspellscreen
  244.      
  245.    if show("l", "rexxarplib.library") = 0 then do
  246.       check = addlib('rexxsupport.library', 0, -30, 0)
  247.       check = addlib('rexxarplib.library',  0, -30, 0)
  248.       check = addlib('rexxmathlib.library', 0, -30, 0)
  249.    end
  250. /*
  251. *   Set the TTX window size such that there is room for the requester.
  252. */
  253.    "sizewindow 0 -55"
  254.    "movewindow 0 55"
  255.      
  256.    mp = OpenPort(MYSPELLPORT)
  257. /*
  258. *   Set up a host. This time, send all messages to MYSPELLPORT.
  259. */
  260.    address AREXX "'x = CreateHost(SPELLREQUEST, MYSPELLPORT, "myspellscreen")'"
  261. /*
  262. *   Wait until it is ready.
  263. */
  264.    do i = 1
  265.       if showlist('p', SPELLREQUEST) ~= 0 then leave
  266.       call delay 30
  267.    end
  268. /*
  269. *   Open the window
  270. */
  271.    idcmp = 'CLOSEWINDOW+GADGETUP+MENUPICK'
  272.    flags = 'WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH+ACTIVATE'
  273.      
  274.    call OpenWindow(SPELLREQUEST, 0, 0, cols, 55, idcmp, flags, "ISpell Control")
  275. /*
  276. *   Add the gadgets
  277. */
  278.    call AddMenu(SPELLREQUEST, "Commands")
  279.      
  280.    call AddItem(SPELLREQUEST, " Add to Temporary Dictionary",    "ADD", "A", 0)
  281.    call AddItem(SPELLREQUEST, " Go to Next Error",            "IGNORE", "N", 0)
  282.    call AddItem(SPELLREQUEST, " Select Alternatives",         "SELECT", "L", 0)
  283.    call AddItem(SPELLREQUEST, " Search/Replace",                  "SR", "R", 0)
  284.    call AddItem(SPELLREQUEST, " Save Dictionary Additions",     "SAVE", "S", 0)
  285.    call AddItem(SPELLREQUEST, " Exit ISpell",                   "EXIT", "E", 0)
  286.    call AddItem(SPELLREQUEST, " Leave (ISpell stays running)", "CLOSEWINDOW", "Q", 0)
  287.      
  288.    call AddGadget(SPELLREQUEST,  20, 17, 1, " Add ",                      "ADD")
  289.    call AddGadget(SPELLREQUEST,  70, 17, 2, " Next ",                  "IGNORE")
  290.    call AddGadget(SPELLREQUEST, 128, 17, 3, " Select ",                "SELECT")
  291.    call AddGadget(SPELLREQUEST, 222, 17, 4, " S/R ",                       "SR")
  292.    call AddGadget(SPELLREQUEST, 292, 17, 5, " Save Additions ",          "SAVE")
  293.    call AddGadget(SPELLREQUEST, 430, 17, 6, " Exit ISpell ",             "EXIT")
  294.    call AddGadget(SPELLREQUEST, 544, 17, 7, " Leave ",            "CLOSEWINDOW")
  295.      
  296. /*
  297. *   Display a "Checking..." string
  298. */
  299.    call SpellMsg("Checking...")
  300.      
  301.    return 1
  302.      
  303. /** SpellMsg
  304. *
  305. *   Display a line in the spell requester
  306. *
  307. **/
  308. SpellMsg: Procedure
  309.    parse arg line
  310.      
  311.    call WindowText(SPELLREQUEST, "\\"line)
  312.      
  313.    return
  314.      
  315. /** GetNextMsg
  316. *
  317. *   GetNextMsg gets the next message from MYSPELLPORT. Arguments are
  318. *   the word that led to this call and the string we got from ISpell.
  319. *
  320. **/
  321. GetNextMsg: Procedure
  322.    parse arg item, string, alternatives
  323. /*
  324. *   Display the string, after wrapping it if needed.
  325. */
  326.    call SpellMsg(string)
  327. /*
  328. *   Make a list of alternatives
  329. */
  330.    if alternatives ~= "" then do
  331.       wordlist.0 = words(alternatives)
  332.       do i = 1 to wordlist.0
  333.          wordlist.i = word(alternatives, i)
  334.       end
  335.       call SetGadget(SPELLREQUEST, 3, ON)
  336.    end
  337. /*
  338. *   In case the user wants to do a search/replace, set up the find string
  339. */
  340.    "SetPrefs FindString "item
  341.      
  342. restart:
  343. /*
  344. *   Check if there are any more messages. This procedure does not
  345. *   WaitPkt unless there aren't any messages.
  346. */
  347.    do i = 1
  348.       p = getpkt(MYSPELLPORT)
  349.       if p = NULL() then do
  350.          call waitpkt(MYSPELLPORT)
  351.          p = getpkt(MYSPELLPORT)
  352.          if p = NULL() then iterate i
  353.       end
  354.       leave i
  355.    end
  356.    call SetGadget(SPELLREQUEST, 3, OFF)
  357. /*
  358. *   There's a message.
  359. */
  360.    thisarg = getarg(p)
  361.    t = reply(p, 0)
  362. /*
  363. *   Do what needs to be done
  364. */
  365.    select
  366.       when (thisarg = "CLOSEWINDOW") | (thisarg = "EXIT") then do
  367.          if thisarg = "EXIT" then address "IRexxSpell" exit
  368.          return 0
  369.       end
  370.       when thisarg = "IGNORE" then do
  371.          call SpellMsg("Continuing...")
  372.          return 1
  373.       end
  374.       when thisarg = "ADD"    then do
  375.          address "IRexxSpell" quickadd item
  376.          call SpellMsg("Continuing...")
  377.          return 1
  378.       end
  379.       when thisarg = "SAVE"   then do
  380.          address "IRexxSpell" add
  381.          call SpellMsg("Continuing...")
  382.          return 1
  383.       end
  384.       when thisarg = "SR"     then do
  385.          "OpenRequester FindChange"
  386.          signal restart:
  387.       end
  388.       when thisarg = "SELECT" then do
  389.          if show('p', 'QuickSortPort') & (alternatives ~= "") then do
  390. /*
  391. *   Get the screen size and name according to TTX. Set the TTX window
  392. *   size such that there is room for the requester.
  393. */
  394.             "getscreeninfo"
  395.             info = result
  396.             parse var info x x x x x cols rows myspellscreen
  397.      
  398.             call ListRequest(1, wordlist.0, wordlist, selected, 128, 30,  ,
  399.                              280, 170, myspellscreen, SORT)
  400.             if selected ~= "" then do
  401.                "SetPrefs ChangeString "selected
  402.                "GetWord"
  403.                if result = item then  do
  404.                   "ReplaceWord" selected
  405.                   line = "Replaced."
  406.                end
  407.                else do
  408.                   line = "Word under cursor not the same! Not corrected."
  409.                end
  410.                call SpellMsg(line)
  411.             end
  412.          end
  413.          else do
  414.             call SpellMsg("No alternatives found.")
  415.          end
  416.          signal restart:
  417.       end
  418.    end
  419.